  #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
  
  CBUFFER_START(UnityPerMaterial)
    float4 _CustomCubeTex_HDR;

   
    half4 _BaseColor;

    #ifdef TYPE_HAIR
     half _NewType;
    half4 _BaseColor1;
    half4 _BaseColor2;
    half4 _BaseColor3;
    half4 _BaseColor4;
    half4 _BaseColor5;

    #endif

    half4 _BaseMap_ST;
    half _BumpScale;
    half _OcclusionStrength;
 
    half4 _EmissionColor;
    half _Cutoff;
    half _Smoothness;
    half _Metallic;


    half _Com01Enable;
    half4 _Com01Map_ST;
    half _Com02Enable;
    half4 _Com02Map_ST;

    half4 _Com01Color;
    half4 _Com02Color;

    #define _BackLight 1
    //half _BackLight;
   // #ifdef _SSS

    half4 _SssColor;
 
    //half _SssAmb;
 

    half _PrimaryShift;
 
    half _AnisoPower;
    half4 _AnisoGlass; 
    half _AnisoDir;
    half _SpecularMaxLimit;
    half4 _ClipMark;
    half _CustomCubeTexBrightness;


    #if defined(_DETAIL)
    half _DetailUsedUV2;
    half4 _DetailAlbedoMap_ST;
    half4 _DetailNormalMap_ST;
    half _DetailAlbedoMapScale;
    half _DetailNormalMapScale;
    half _DetalDiffCtrl;
    half _DetalBumpCtrl;
    half _DetailMetallic;
    half _DetailSmoothness;
    half _DetailAlbedoMapBumpScale;
    #endif
    //#endif
CBUFFER_END
TEXTURE2D(_BaseMap);SAMPLER(sampler_BaseMap);
TEXTURE2D(_Com01Map);SAMPLER(sampler_Com01Map);
TEXTURE2D(_Com02Map);SAMPLER(sampler_Com02Map);

TEXTURE2D(_MetallicGlossMap);SAMPLER(sampler_MetallicGlossMap);
TEXTURE2D(_BumpMap);        SAMPLER(sampler_BumpMap);
TEXTURE2D(_FlowMap);        SAMPLER(sampler_FlowMap);
#if defined(_DETAIL)


TEXTURE2D(_DetailMask);         SAMPLER(sampler_DetailMask);
TEXTURE2D(_DetailAlbedoMap);    SAMPLER(sampler_DetailAlbedoMap);
TEXTURE2D(_DetailAlbedoMapDump);    SAMPLER(sampler_DetailAlbedoMapDump);
 
TEXTURE2D(_DetailNormalMap);    SAMPLER(sampler_DetailNormalMap);
#if defined(_DETAIL)
half3 ApplyDetailAlbedo(float2 detailUv, half3 albedo, half detailMask , inout float  smoothness,inout float metallic,inout float3 normalTS)
{

    half4 detailAlbedo = SAMPLE_TEXTURE2D(_DetailAlbedoMap, sampler_DetailAlbedoMap, detailUv);

    float mark = detailAlbedo.a * _DetailAlbedoMapScale;
    

    smoothness = lerp(smoothness,_DetailSmoothness,mark);
    metallic = lerp(metallic,_DetailMetallic,mark);
   

   #if BUMP_SCALE_NOT_SUPPORTED
    half3 detailNormalTS = UnpackNormal(SAMPLE_TEXTURE2D(_DetailAlbedoMapDump, sampler_DetailAlbedoMapDump, detailUv));
#else
    half3 detailNormalTS = UnpackNormalScale(SAMPLE_TEXTURE2D(_DetailAlbedoMapDump, sampler_DetailAlbedoMapDump, detailUv), _DetailNormalMapScale);
#endif

    detailNormalTS = normalize(detailNormalTS);
 
    normalTS = lerp(normalTS,detailNormalTS,mark);
    return lerp(albedo,detailAlbedo.rgb,mark);
    

}
#endif

half3 ApplyDetailNormal(float2 detailUv, half3 normalTS, half detailMask)
{
#if defined(_DETAIL)
#if BUMP_SCALE_NOT_SUPPORTED
    half3 detailNormalTS = UnpackNormal(SAMPLE_TEXTURE2D(_DetailNormalMap, sampler_DetailNormalMap, detailUv));
#else
    half3 detailNormalTS = UnpackNormalScale(SAMPLE_TEXTURE2D(_DetailNormalMap, sampler_DetailNormalMap, detailUv), _DetailNormalMapScale);
#endif

    // With UNITY_NO_DXT5nm unpacked vector is not normalized for BlendNormalRNM
    // For visual consistancy we going to do in all cases
    detailNormalTS = normalize(detailNormalTS);

    return lerp(normalTS, BlendNormalRNM(normalTS, detailNormalTS), detailMask); // todo: detailMask should lerp the angle of the quaternion rotation, not the normals
#else
    return normalTS;
#endif
}
#endif


half Alpha(half albedoAlpha, half4 color, half cutoff)
{
#if !defined(_SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A) && !defined(_GLOSSINESS_FROM_BASE_ALPHA)
    half alpha = albedoAlpha * color.a;
#else
    half alpha = color.a;
#endif

 
#if defined(_ALPHATEST_ON)
    clip(alpha - cutoff);
#endif

 

    return alpha;
}
half4 SampleAlbedoAlpha(float2 uv, TEXTURE2D_PARAM(albedoAlphaMap, sampler_albedoAlphaMap))
{
    return half4(SAMPLE_TEXTURE2D(albedoAlphaMap, sampler_albedoAlphaMap, uv));
}
 
 #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
 #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonMaterial.hlsl"
 #include "lch_pbr.hlsl"


 #if defined(  ANISOTROPIC_FLOWMAP)
    #define BITANGENT_SPACE  
    #define FLOWMAP   
#endif








#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Packing.hlsl"


 

half3 SampleNormal(float2 uv, TEXTURE2D_PARAM(bumpMap, sampler_bumpMap), half scale = half(1.0))
{
#ifdef _NORMALMAP
    half4 n = SAMPLE_TEXTURE2D(bumpMap, sampler_bumpMap, uv);
    /*#if BUMP_SCALE_NOT_SUPPORTED
        return UnpackNormal(n);
    #else*/
        return UnpackNormalScale(n, scale);
   // #endif
#else
    return half3(0.0h, 0.0h, 1.0h);
#endif
}
            
half3 GetNormalST(float2 uv)
{
    #ifdef _NORMALMAP
    return SampleNormal(uv, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap), _BumpScale);
    #else
    return  half3(0,0,1);
    #endif
}

half4 SampleMetallicSpecGloss(float2 uv, half albedoAlpha)
{
    half4 specGloss;
    specGloss = SAMPLE_TEXTURE2D(_MetallicGlossMap, sampler_MetallicGlossMap, uv);
    #if _SPECULAR_SETUP
        specGloss.rgb = _SpecColor.rgb;
    #else
        #if _SSS
        specGloss.r = specGloss.r;
        #else
        specGloss.r = _Metallic*specGloss.r;
        #endif
        
    #endif
    specGloss.a = _Smoothness*specGloss.a;
     
    return specGloss;
}



#if defined( ANISOTROPIC_ON )||defined(  ANISOTROPIC_FLOWMAP)
    #define BITANGENT_SPACE   
#endif
#if defined(  ANISOTROPIC_FLOWMAP)
    #define BITANGENT_SPACE  
    #define FLOWMAP   
#endif
inline void InitializeLchSurfaceData(float2 uv,float2 uv2,float4 vertexColor, out LchSurfaceData outSurfaceData)
{
   
    half4 albedoAlpha = SAMPLE_TEXTURE2D( _BaseMap, sampler_BaseMap,uv);
    outSurfaceData.alpha = Alpha(albedoAlpha.a, _BaseColor, _Cutoff);
    half4 specGloss = SampleMetallicSpecGloss(uv, albedoAlpha.a);

    #ifdef TYPE_HAIR
        if(_NewType)
        {
            //_NewType
            outSurfaceData.albedo = albedoAlpha.r ;
            if(vertexColor.r)
            {
                outSurfaceData.albedo.rgb *= lerp(_BaseColor1.rgb ,_BaseColor.rgb,albedoAlpha.g);
            }
            else if(vertexColor.g)
            {
                outSurfaceData.albedo.rgb *= lerp(_BaseColor3.rgb ,_BaseColor2.rgb,albedoAlpha.g);
            }
            else  
            {
                outSurfaceData.albedo.rgb *= lerp(_BaseColor5.rgb ,_BaseColor4.rgb,albedoAlpha.g);
            }
            
            // _BaseColor.rgb
        }
        else
        {
            outSurfaceData.albedo = albedoAlpha.r * _BaseColor.rgb;
        }
        
    #else
        outSurfaceData.albedo = albedoAlpha.rgb * _BaseColor.rgb;
    #endif
    
    


    #if _SSS
    outSurfaceData.metallic = 0.0;
    #else
    outSurfaceData.metallic = specGloss.r*0.8+0.2;
    //outSurfaceData.metallic = specGloss.r;
    #endif
    
    outSurfaceData.smoothness = specGloss.a;
    outSurfaceData.normalTS = GetNormalST(uv);
    outSurfaceData.occlusion =  1.0f + _OcclusionStrength * (specGloss.g - 1.0f);
    outSurfaceData.emission = outSurfaceData.albedo.rgb*_EmissionColor.rgb*specGloss.b;

    #if defined(TYPE_HAIR)
          
    #else
    if(_Com01Enable>0.5)
    {
        half4 _com01Map = SAMPLE_TEXTURE2D( _Com01Map,  sampler_Com01Map,uv*_Com01Map_ST.xy+_Com01Map_ST.zw);
        float g1 = dot(_com01Map.rgb,half3(0.3,0.6,0.1));
        half3 newCol1 = _Com01Color.rgb*g1;
        _com01Map.rgb = lerp(_com01Map.rgb, newCol1,_Com01Color.a);

    
  
        outSurfaceData.albedo = lerp(outSurfaceData.albedo,_com01Map.rgb,_com01Map.a);
    }
    if(_Com02Enable>0.5)
    {
        half4 _com02Map = SAMPLE_TEXTURE2D( _Com02Map,  sampler_Com02Map,uv*_Com02Map_ST.xy+_Com02Map_ST.zw);

        float g2 = dot(_com02Map.rgb,half3(0.3,0.6,0.1));
        half3 newCol2 = _Com02Color.rgb*g2;
        _com02Map.rgb = lerp(_com02Map.rgb, newCol2,_Com02Color.a);

        outSurfaceData.albedo = lerp(outSurfaceData.albedo,_com02Map.rgb,_com02Map.a);

         
    }
    
    #endif

    // TYPE_NONE TYPE_FACE TYPE_HAIR

    #ifdef _SSS
    outSurfaceData.sssColor = _SssColor*specGloss.r;
 
    outSurfaceData.sssAmb = 1.0;
    
 
    #endif

 
    #if defined(ANISOTROPIC_ON)  
        outSurfaceData.anisoGlass = _AnisoGlass.rgb ;
        outSurfaceData.primaryShift = _PrimaryShift;
 
        outSurfaceData.anisoPower = _AnisoPower;
        if(_AnisoDir>1.5)
        {
             half4 n = SAMPLE_TEXTURE2D(_FlowMap, sampler_FlowMap, uv);
             outSurfaceData.anisoDirTS =  n.rgb*2-1;
        } 
        else if(_AnisoDir>0.5)
        {
            outSurfaceData.anisoDirTS = half3(1.0,0.0,0.0);
        }
        else
        {
            outSurfaceData.anisoDirTS = half3(0.0,-1.0,0.0);
        }
    #else
        outSurfaceData.anisoGlass = 0.0;
        outSurfaceData.primaryShift = 0.0;
        outSurfaceData.anisoPower = 0.0;
        outSurfaceData.anisoDirTS = half3(0.0,1.0,0.0);
 
    #endif
        outSurfaceData.backLight = _BackLight;


    #if defined(_DETAIL)
    half detailMask = SAMPLE_TEXTURE2D(_DetailMask, sampler_DetailMask, uv).a;
    float2 detailUv = _DetailUsedUV2? uv2 * _DetailAlbedoMap_ST.xy + _DetailAlbedoMap_ST.zw :   uv * _DetailAlbedoMap_ST.xy + _DetailAlbedoMap_ST.zw;
    
    
    if( _DetalBumpCtrl)
    {
        float2 detailNormalUV = uv * _DetailNormalMap_ST.xy + _DetailNormalMap_ST.zw;  
        outSurfaceData.normalTS = ApplyDetailNormal(detailNormalUV, outSurfaceData.normalTS, detailMask);
    }
    if(_DetalDiffCtrl)
    {
        outSurfaceData.albedo = ApplyDetailAlbedo(detailUv, outSurfaceData.albedo, detailMask  ,outSurfaceData.smoothness,outSurfaceData.metallic,outSurfaceData.normalTS);
    }
     
    #endif

    outSurfaceData.maxLimit = _SpecularMaxLimit;
 
} 

